//
//  HWWuKongDetailView.swift
//  HWanGame
//
//  Created by Apple on 2024/8/15.
//  Copyright © 2024 94Wan. All rights reserved.
//

import UIKit
import Stevia
import Cosmos

//MARK: =================
class HWWuKongDetailTopView: UIView {

    private let iconImageView = c_make_ex(UIImageView.self)
        .corner(12*widthScale).mode(.scaleAspectFill).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .font(16.font_semiBold).color(.white).end()
    private let subLabel = c_make_ex(UILabel.self)
        .font(12.font_medium).color("#EDEEF0").end()
    private let logoLabel =  c_make_ex(UILabel.self)
        .color(.white).font(8.font_wide).text("77TOP")
        .alignment(.right).end()
    private let scoreLabel =  c_make_ex(UILabel.self)
        .color(.white).font(16.font_wide).alignment(.right).end()
    var starView = CosmosView()
    var topModel: HWGameDetailModel? {
        didSet{
            guard let md = topModel else { return }
            iconImageView.sd_setImage(with: URL(string: md.icon))
            nameLabel.text = md.name
            scoreLabel.text = "\(md.point)"
            starView.rating = md.point
            subLabel.text = md.type
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .clear
        
        subviews{
            iconImageView
            nameLabel
            subLabel
            logoLabel
            scoreLabel
            starView
        }
        
        iconImageView.leading(12*widthScale).top(8*widthScale).size(60*widthScale)
        nameLabel.leading(80*widthScale).top(10*widthScale).trailing(75*widthScale)
        subLabel.leading(80*widthScale).top(30*widthScale).trailing(75*widthScale)
        logoLabel.trailing(22*widthScale).top(18*widthScale)
        scoreLabel.trailing(22*widthScale).top(25*widthScale)
        starView.leading(80*widthScale)
        starView.Bottom == iconImageView.Bottom
        
        starView.rating = 0
        starView.settings.filledColor = .colorWithHexString(hexString: "#FF7500")
        starView.settings.emptyColor = "#E4E5E6".toColor
        starView.settings.starSize = 15*widthScale
        starView.settings.starMargin = 0
        starView.isUserInteractionEnabled = false
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

//MARK: =================
class HWWuKongDetailHeaderView: UITableViewHeaderFooterView {
         
    private let titleLabel = c_make_ex(UILabel.self)
        .font(15.font_semiBold).color(.white).end()
    
    var titleStr: String = "" {
        didSet{
            titleLabel.text = titleStr
        }
    }
    
    override init(reuseIdentifier: String?) {
        super.init(reuseIdentifier: reuseIdentifier)
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        backgroundView = UIView()
        backgroundView?.backgroundColor = .clear
        
        subviews{
            titleLabel
        }

        titleLabel.leading(12*widthScale).top(8*widthScale)
    }

    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func layoutSubviews() {
        super.layoutSubviews()
    }
    
}


//MARK: =================
class HWWuKongDetailGamePicCell: UITableViewCell, UICollectionViewDelegate, UICollectionViewDataSource {
    
    private let collectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
    private let backView_one = c_make_ex(UIView.self)
        .bgColor(.black.alpha(0.2)).corner(12*widthScale).end()
    private let backView_two = c_make_ex(UIView.self)
        .bgColor(.black.alpha(0.2)).corner(12*widthScale).end()
    private let backView_three = c_make_ex(UIView.self)
        .bgColor(.black.alpha(0.2)).corner(12*widthScale).end()
    private let backView_four = c_make_ex(UIView.self)
        .bgColor(.black.alpha(0.2)).corner(12*widthScale).end()
    private let recommendImg = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("Detail_recommend").end()
    private let title_two = c_make_ex(UILabel.self)
        .font(14.font_semiBold).color(.white).alignment(.center).end()
    private let sub_two = c_make_ex(UILabel.self)
        .font(12.font_medium).text("游戏大小")
        .color(.white.alpha(0.5)).alignment(.center).end()
    private let title_three = c_make_ex(UILabel.self)
        .font(14.font_semiBold).color(.white).alignment(.center).end()
    private let sub_three = c_make_ex(UILabel.self)
        .font(12.font_medium).text("预约榜")
        .color(.white.alpha(0.5)).alignment(.center).end()
    private let title_four = c_make_ex(UILabel.self)
        .font(14.font_semiBold).color(.white).alignment(.center).end()
    private let sub_four = c_make_ex(UILabel.self)
        .font(12.font_medium).text("关注数")
        .color(.white.alpha(0.5)).alignment(.center).end()
    
        
    var gameModel: HWGameDetailModel? {
        didSet{
            guard let md = gameModel else { return }
            title_two.text = "\(md.size)M"
            title_three.text = md.orderby
            title_four.text = md.follow
        }
    }
    
    var imageModel: HWGameDetailInfoModel? {
        didSet{
            guard let _ = imageModel else { return }
            collectionView.reloadData()
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        subviews{
            collectionView
            backView_one.subviews{
                recommendImg
            }
            backView_two.subviews{
                title_two
                sub_two
            }
            backView_three.subviews{
                title_three
                sub_three
            }
            backView_four.subviews{
                title_four
                sub_four
            }
            
        }
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 12*widthScale
        layout.minimumInteritemSpacing = 12*widthScale
        layout.scrollDirection = .horizontal
        layout.itemSize = CGSizeMake(280*widthScale, 160*widthScale)
        layout.sectionInset = UIEdgeInsets(top: 0, left: 12*widthScale, bottom: 0, right: 0)
        
        collectionView.collectionViewLayout = layout
        collectionView.backgroundColor = .clear
        collectionView.register(HWWuKongDetailGamePicItemCell.self, forCellWithReuseIdentifier: HWWuKongDetailGamePicItemCell.description())
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.delegate = self
        collectionView.dataSource = self
        
        let width = (CGFloat.screenWidth-60*widthScale)/4
        collectionView.leading(0).trailing(0).height(160*widthScale).top(8*widthScale)
        backView_one.leading(12*widthScale).height(60*widthScale).width(width).bottom(8*widthScale)
        backView_one.Top == collectionView.Bottom + 15*widthScale
        recommendImg.height(32*widthScale).width(65*widthScale).centerVertically().centerHorizontally()
        backView_two.height(60*widthScale).width(width)
        backView_two.CenterY == backView_one.CenterY
        backView_two.Leading == backView_one.Trailing + 12*widthScale
        title_two.leading(5).trailing(5).top(13*widthScale)
        sub_two.leading(5).trailing(5).bottom(13*widthScale)
        backView_three.height(60*widthScale).width(width)
        backView_three.CenterY == backView_two.CenterY
        backView_three.Leading == backView_two.Trailing + 12*widthScale
        title_three.leading(5).trailing(5).top(13*widthScale)
        sub_three.leading(5).trailing(5).bottom(13*widthScale)
        backView_four.height(60*widthScale).width(width)
        backView_four.CenterY == backView_two.CenterY
        backView_four.Leading == backView_three.Trailing + 12*widthScale
        title_four.leading(5).trailing(5).top(13*widthScale)
        sub_four.leading(5).trailing(5).bottom(13*widthScale)
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageModel?.imgs.imgs.count ?? 0
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: HWWuKongDetailGamePicItemCell.description(), for: indexPath) as? HWWuKongDetailGamePicItemCell else { return UICollectionViewCell() }
        cell.imgStr = imageModel?.imgs.imgs[indexPath.row]
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}


class HWWuKongDetailGamePicItemCell: UICollectionViewCell {
    
    private let iconImageView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).corner(12*widthScale).end()
    
    var imgStr: String? {
        didSet{
            guard let url = imgStr else { return }
            iconImageView.sd_setImage(with: URL(string: url))
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        contentView.backgroundColor = .clear
        backgroundColor = .clear
        
        contentView.subviews{
            iconImageView
        }
                
        iconImageView.fillContainer()
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

//MARK: =================
class HWWuKongDetailGameInfoCell: UITableViewCell {

    private let contentLabel = c_make_ex(UILabel.self)
        .color(.white).font(14.font_medium).line(3).end()
    private let moreButton = UIIconButton(style: .trailing(itemSpace: 2))
    
    var model: HWGameDetailInfoModel? {
        didSet{
            guard let md = model else { return }
            contentLabel.text = md.content
            
            if getTextHeigh(textStr: contentLabel.text!, font: 14.font_medium!, width: screenWidth-24*widthScale) > 56 {
                moreButton.heightConstraint?.constant = 32*widthScale
                moreButton.isHidden = false
                moreButton.isSelected = md.isOpen
                if md.isOpen {
                    contentLabel.numberOfLines = 0
                } else {
                    contentLabel.numberOfLines = 3
                }
            }else {
                moreButton.heightConstraint?.constant = 0
                moreButton.isHidden = true
            }
        }
    }
    
    var showMoreInfo: (() -> Void)?

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            contentLabel
            moreButton
        }
           
        moreButton.style { v in
            v.iconSize = CGSize(width: 13*widthScale, height: 13*widthScale)
            v.setImage("Detail_down".toImage, for: .normal)
            v.setImage("Detail_up".toImage, for: .selected)
            v.setTitleColor("#AEB0B3".toColor, for: .normal)
            v.titleLabel?.font = 12.font_medium
            v.setTitle("点击展开全部简介", for: .normal)
            v.setTitle("点击收起全部简介", for: .selected)
            v.addTarget(self, action: #selector(clickMoreButton), for: .touchUpInside)
        }
        
        contentLabel.leading(12*widthScale).trailing(12*widthScale).top(0)
        moreButton.leading(0).trailing(0).height(32*widthScale).bottom(0)
        moreButton.Top == contentLabel.Bottom + 4*widthScale
    }
    
    @objc private func clickMoreButton(){
        
//        moreButton.isSelected = !moreButton.isSelected
        self.model?.isOpen = !(self.model?.isOpen ?? false)
        showMoreInfo?()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}


//MARK: =================
class HWWuKongDetailWelfareCell: UITableViewCell {

    private let contentImageV = c_make_ex(UIImageView.self)
        .corner(12*widthScale).mode(.scaleAspectFill).end()

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            contentImageV
        }
        
        contentImageV.leading(12*widthScale).trailing(12*widthScale).top(8*widthScale).bottom(8*widthScale).height(350*widthScale)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

//MARK: =================
class HWWuKongDetailCommitCell: UITableViewCell {
    
    private let bgView = c_make_ex(UIView.self)
        .bgColor(.white.alpha(0.9)).corner(12*widthScale).end()
    private let iconImageView = c_make_ex(UIImageView.self)
        .corner(20*widthScale).mode(.scaleAspectFill).end()
    private let nameLabel = c_make_ex(UILabel.self)
        .color(.black).font(12.font_semiBold).end()
    private let timeLabel = c_make_ex(UILabel.self)
        .color("#AEB0B3").font(11.font_medium).end()
    private let contentLabel = c_make_ex(UILabel.self)
        .color("#47494D").font(15.font_medium)
        .line(0).end()
    private let subLabel = c_make_ex(UILabel.self)
        .color("#7B7D80").font(10.font_medium).alignment(.right).end()
    private let likeButton = c_make_ex(UIButton.self)
        .image("WuKong_like").image("Detail_like_sel", state: .selected).end()
    var starView = CosmosView()

    var clickLikeBlock: ((_ id: String) -> Void)?
    
    var model: HWGameDetailCommentListModel? {
        didSet{
            guard let md = model else { return }
            
            iconImageView.sd_setImage(with: URL(string: md.touxiang))
            contentLabel.text = md.content
            nameLabel.text = md.user_name
            timeLabel.text = md.time
            subLabel.text = md.num + "评论 · " + md.zan + "点赞"
            starView.rating = md.point
            likeButton.isSelected = md.is_zan
        }
        
    }
    
    class func cellWithTableView(tableView:UITableView) -> (HWGameCommentCell) {
        let reUse:String = "HWGameCommentCell"
        
        var cell = tableView.dequeueReusableCell(withIdentifier: reUse)
        
        if cell == nil {
            cell = HWGameCommentCell.init(style: UITableViewCell.CellStyle.default, reuseIdentifier: reUse)
        }
        
        return cell as! HWGameCommentCell
    }
    
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        initUI()
    }
    
    func initUI() -> Void {
        selectionStyle = UITableViewCell.SelectionStyle.none
        backgroundColor = UIColor.clear
        
        contentView.subviews{
            bgView.subviews{
                iconImageView
                nameLabel
                timeLabel
                contentLabel
                subLabel
                likeButton
                starView
            }
        }
        
        bgView.leading(12*widthScale).trailing(12*widthScale).top(0).bottom(12*widthScale)
        iconImageView.leading(12*widthScale).top(16*widthScale).size(40*widthScale)
        nameLabel.leading(64*widthScale).top(18*widthScale).trailing(15*widthScale)
        timeLabel.leading(64*widthScale).top(36*widthScale).trailing(15*widthScale)
        starView.leading(64*widthScale).top(54*widthScale)
        contentLabel.leading(64*widthScale).top(73*widthScale).trailing(16*widthScale)
        likeButton.trailing(8*widthScale).size(44*widthScale).bottom(10*widthScale)
        likeButton.Top == contentLabel.Bottom
        subLabel.trailing(56*widthScale)
        subLabel.CenterY == likeButton.CenterY
        
        starView.rating = 0
        starView.settings.filledColor = .colorWithHexString(hexString: "#FF7500")
        starView.settings.emptyColor = "#E4E5E6".toColor
        starView.settings.starSize = 15*widthScale
        starView.settings.starMargin = 0
        starView.isUserInteractionEnabled = false
        
        likeButton.addTarget(self, action: #selector(clickLikeButton), for: .touchUpInside)
    }
    
    @objc private func clickLikeButton(){
        clickLikeBlock?(model?.id ?? "")
    }

    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
//    @objc func buttonClick(_ button:UIButton) -> Void {
//        photoBroseView.imagesURL = model?.images
//        photoBroseView.frameFormWindow = CGRect(x: screenWidth/2, y: screenHeight/2, width: 0, height: 0)
//        photoBroseView.frameToWindow = CGRect(x: screenWidth/2, y: screenHeight/2, width: 0, height: 0)
//        photoBroseView.currentIndex = button.tag - 9999
//        photoBroseView.show()
//    }
//
//    @objc func zanButtonClick(_ button:UIButton) -> Void {
//        self.zanButtonClick?(model?.id ?? "")
//    }
//
//    @objc func videoButtonClick(_ button:UIButton) -> Void {
//        self.videoPlayClick?(self.model?.video ?? "")
//    }

}

//MARK: =================
class HWWuKongDetailBottomView: UIView {

    private let saysButton = c_make_ex(UIButton.self)
        .corner(17*widthScale).bgColor(.black.alpha(0.05))
        .color("#7B7D80").font(13.font_medium)
        .text("写点什么...").end()
    private let collectButton = UIIconButton(style: .top(itemSpace: 2))
    private let commitButton = UIIconButton(style: .top(itemSpace: 2))

    var clickCommitBlock: (() -> Void)?
    var clickCollectBlock: (() -> Void)?
    
    var model: HWGameDetailModel? {
        didSet{
            guard let md = model else { return }
            collectButton.isSelected = md.shoucang == 1
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        backgroundColor = .white.alpha(0.8)
        
        subviews{
            saysButton
            collectButton
            commitButton
        }
        
        collectButton.style { v in
            v.iconSize = CGSize(width: 24*widthScale, height: 24*widthScale)
            v.setImage("Detail_collect_nor".toImage, for: .normal)
            v.setImage("Detail_collect_sel".toImage, for: .selected)
            v.setTitle("收藏", for: .normal)
            v.setTitleColor("#2E3033".toColor, for: .normal)
            v.setTitleColor("#FF7600".toColor, for: .selected)
            v.titleLabel?.font = 10.font_medium
            v.addTarget(self, action: #selector(clickCollectButton), for: .touchUpInside)
        }
        commitButton.style { v in
            v.iconSize = CGSize(width: 24*widthScale, height: 24*widthScale)
            v.setImage("Detail_commit_nor".toImage, for: .normal)
            v.setImage("Detail_commit_sel".toImage, for: .selected)
            v.setTitle("评论", for: .normal)
            v.setTitleColor("#2E3033".toColor, for: .normal)
            v.setTitleColor("#FF7600".toColor, for: .selected)
            v.titleLabel?.font = 10.font_medium
            v.addTarget(self, action: #selector(clickCommitButton), for: .touchUpInside)
        }
        
        collectButton.top(4*widthScale).size(44*widthScale).trailing(12*widthScale)
        commitButton.top(4*widthScale).size(44*widthScale).trailing(56*widthScale)
        saysButton.leading(12*widthScale).height(34*widthScale).width(250*widthScale).top(9*widthScale)
        
        saysButton.addTarget(self, action: #selector(clickCommitButton), for: .touchUpInside)
    }
    
    @objc private func clickCommitButton(){
        clickCommitBlock?()
    }
    
    @objc private func clickCollectButton(){
        clickCollectBlock?()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
